#include "OLSplashScreen.h"
#include "OLApplication.h"

#include <QtGui/QPainter>
#include <QTimer>
#include <QPropertyAnimation>
#include <QResizeEvent>
#include <QLayout>
#include <QParallelAnimationGroup>

OLSplashScreen::OLSplashScreen(QWidget *parent) :
        QWidget(parent), m_opacity(1.0), m_logo(":/gfx/gfx_wait")
{
    QTimer::singleShot(2000, this, SLOT(handleTimer()));
}

void OLSplashScreen::resizeEvent(QResizeEvent *e)
{
    if(!m_image.isNull())
        m_scaledimage = m_image.scaled(e->size());
}

void OLSplashScreen::paintEvent(QPaintEvent* e)
{
    QPainter p(this);
    if(m_oldscaledimage.isNull())
        p.setOpacity(m_opacity);
    else
        p.setOpacity(1.0);

    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.drawRect(rect());

    if(!m_oldscaledimage.isNull()) {
        p.setOpacity(1.0-m_opacity);
        p.drawPixmap(rect(), m_oldscaledimage);
    }

    if(!m_image.isNull()) {
        if(!m_oldscaledimage.isNull())
            p.setOpacity(m_opacity);

        p.drawPixmap(rect(), m_scaledimage);
    } else if(!layout()) {
        QSize logoSize = m_logo.size();

        int y = (height()-logoSize.height())/2;
        int x = (width()-logoSize.width())/2;
        p.drawPixmap(x, y, m_logo);

        p.setPen(Qt::white);
        y += (logoSize.height()/2);
        p.drawText(0,y,width(), 200, Qt::AlignCenter, "Loading.....");
    }
}

void OLSplashScreen::changeSplashScreenImage(const QString& pixmap)
{
    delete layout();
    QPixmap newImage(pixmap);
    QPixmap newScaledImage(newImage.scaled(size()));

    if(m_image.isNull()) {
        m_image = newImage;
        m_scaledimage = newScaledImage;
    } else {
        m_oldscaledimage = m_scaledimage;
        m_image = newImage;
        m_scaledimage = newScaledImage;

        QPropertyAnimation* anim = new QPropertyAnimation(this, "opacity", this);
        anim->setDuration(1000);
        anim->setStartValue(0.0);
        anim->setEndValue(1.0);

        anim->start(QAbstractAnimation::DeleteWhenStopped);
        connect(anim, SIGNAL(finished()), SLOT(imageTransitionDone()));
    }

}

void OLSplashScreen::imageTransitionDone()
{
    m_oldscaledimage = QPixmap();
}

void OLSplashScreen::changeSplashScreenLayout(QLayout* newlayout)
{
    delete layout();
    m_image = QPixmap();
    setLayout(newlayout);
}

void OLSplashScreen::dismissSplashScreen()
{
    QPropertyAnimation* anim = new QPropertyAnimation(this, "opacity", this);
    anim->setDuration(1000);
    anim->setStartValue(1.0);
    anim->setEndValue(0.0);

    anim->start(QAbstractAnimation::DeleteWhenStopped);
    connect(anim, SIGNAL(finished()), SLOT(deleteLater()));
    connect(anim, SIGNAL(finished()), OLApp, SIGNAL(appStarted()));
}

void OLSplashScreen::setOpacity(qreal opacity)
{
    m_opacity = opacity;
    update();
}

qreal OLSplashScreen::opacity()
{
    return m_opacity;
}

void OLSplashScreen::handleTimer()
{
    if(m_image.isNull() && !layout())
        dismissSplashScreen();
}

void OLSplashScreen::handleAppResize()
{
    resize(parentWidget()->size());
}
